<?php

//增加字段(3.0)
function AddF($add,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$tbname=RepPostVar($add['tbname']);
	if(empty($add[f])||empty($add[fname])||!$tid||!$tbname)
	{
		printerror("EmptyF","history.go(-1)");
	}
	CheckLevel($userid,$username,$classid,"f");//验证权限
	//字段是否重复
	if($add[f]=="oldurl"||$add[f]=="tmptime"||$add[f]=="smallurl"||$add[f]=="newsurl"||$add[f]=="titlepicl")
	{
		printerror("ReF","history.go(-1)");
	}
	$s=$empire->query("SHOW FIELDS FROM {$dbtbpre}ecms_".$tbname);
	$b=0;
	while($r=$empire->fetch($s))
	{
		if($r[Field]==$add[f])
		{
			$b=1;
			break;
		}
    }
	if($b)
	{
		printerror("ReF","history.go(-1)");
	}
	$add[fvalue]=ReturnFvalue($add[fvalue]);//初始化值
	//初使值
	if($add[ftype]=="TINYINT"||$add[ftype]=="SMALLINT"||$add[ftype]=="INT"||$add[ftype]=="BIGINT"||$add[ftype]=="FLOAT"||$add[ftype]=="DOUBLE")
	{
		$def=" default '0'";
	}
	elseif($add[ftype]=="VARCHAR")
	{
		$def=" default ''";
	}
	else
	{
		$def='';
	}
	$type=$add[ftype];
	//VARCHAR
	if($add[ftype]=='VARCHAR'&&empty($add[flen]))
	{
		$add[flen]='255';
	}
	//字段长度
	if($add[flen])
	{
		if($add[ftype]!="TEXT"&&$add[ftype]!="MEDIUMTEXT"&&$add[ftype]!="LONGTEXT")
		{
			$type.="(".$add[flen].")";
		}
	}
	$field=$add[f]." ".$type." NOT NULL".$def;
	//新增字段
	$asql=$empire->query("alter table {$dbtbpre}ecms_".$tbname." add ".$field);
	$asql=$empire->query("alter table {$dbtbpre}ecms_".$tbname."_doc add ".$field);
	//采集表
	$asql=$empire->query("alter table {$dbtbpre}ecms_infoclass_".$tbname." add zz_".$add[f]." text not null,add z_".$add[f]." varchar(255) not null,add qz_".$add[f]." varchar(255) not null,add save_".$add[f]." varchar(10) not null;");
	$asql=$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." add ".$field);
	//索引
	if($add[iskey]==1)
	{
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname." ADD INDEX(".$add[f].")");
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname."_doc ADD INDEX(".$add[f].")");
	}
	//替换代码
	$fhtml=GetFform($add[fform],$add[f],$add[fvalue],$add[linkfieldval],$add[fformsize]);
	$cjhtml=GetCjform($add[fform],$add[f]);
	$qfhtml=GetQFform($add[fform],$add[f],$add[fvalue],$add[fformsize]);
	//处理变量
	$add[iscj]=(int)$add[iscj];
	$add[myorder]=(int)$add[myorder];
	$tid=(int)$tid;
	$add[savetxt]=(int)$add[savetxt];
	$add[iskey]=(int)$add[iskey];
	$add[tobr]=(int)$add[tobr];
	$add[dohtml]=(int)$add[dohtml];
	$add[isonly]=(int)$add[isonly];
	$add[samedata]=(int)$add[samedata];
	$sql=$empire->query("insert into {$dbtbpre}enewsf(f,fname,fform,fhtml,fzs,isadd,isshow,iscj,cjhtml,myorder,ftype,flen,dotemp,tid,tbname,savetxt,fvalue,iskey,tobr,dohtml,qfhtml,isonly,linkfieldval,samedata,fformsize) values('$add[f]','$add[fname]','$add[fform]','".addslashes($fhtml)."','".addslashes($add[fzs])."',1,1,$add[iscj],'".addslashes($cjhtml)."',$add[myorder],'$add[ftype]','$add[flen]',1,$tid,'$tbname',$add[savetxt],'".addslashes($add[fvalue])."',$add[iskey],$add[tobr],$add[dohtml],'".addslashes($qfhtml)."','$add[isonly]','".addslashes($add[linkfieldval])."','$add[samedata]','$add[fformsize]');");
	$lastid=$empire->lastid();
	TogSaveTxtF();//公共变量
	if($add[savetxt]==1&&$add[f]=="newstext")//存放文本
	{
		$tmpsql=$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." change newstext newstext mediumtext not null;");
	}
	if($asql&&$sql)
	{
		insert_dolog("fid=".$lastid."<br>f=".$add[f]);//操作日志
		printerror("AddFSuccess","db/AddF.php?enews=AddF&tid=$tid&tbname=$tbname");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//修改数据库字段
function EditF($add,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$tbname=RepPostVar($add['tbname']);
	$add[fid]=(int)$add['fid'];
	if(empty($add[f])||empty($add[fname])||empty($add[fid])||!$tid||!$tbname)
	{
		printerror("EmptyF","history.go(-1)");
	}
	CheckLevel($userid,$username,$classid,"f");//验证权限
	//是否系统内部字段
	$cr=$empire->fetch1("select isadd from {$dbtbpre}enewsf where fid='$add[fid]'");
	if(empty($cr[isadd]))
	{
		printerror("NotIsAdd","history.go(-1)");
	}
	if($add[f]=="oldurl"||$add[f]=="tmptime"||$add[f]=="smallurl"||$add[f]=="newsurl"||$add[f]=="titlepicl")
	{
		printerror("ReF","history.go(-1)");
	}
	if($add[f]<>$add[oldf])
	{
		//字段是否重复
		$s=$empire->query("SHOW FIELDS FROM {$dbtbpre}ecms_".$tbname);
		$b=0;
		while($r=$empire->fetch($s))
		{
			if($r[Field]==$add[f])
			{
				$b=1;
				break;
			}
		}
		if($b)
		{
			printerror("ReF","history.go(-1)");
		}
	}
	$add[fvalue]=ReturnFvalue($add[fvalue]);//初始化值
	//初始值
	if($add[ftype]=="TINYINT"||$add[ftype]=="SMALLINT"||$add[ftype]=="INT"||$add[ftype]=="BIGINT"||$add[ftype]=="FLOAT"||$add[ftype]=="DOUBLE")
	{
		$def=" default '0'";
	}
	elseif($add[ftype]=="VARCHAR")
	{
		$def=" default ''";
	}
	else
	{
		$def='';
	}
	$type=$add[ftype];
	//VARCHAR
	if($add[ftype]=='VARCHAR'&&empty($add[flen]))
	{
		$add[flen]='255';
	}
	//字段长度
	if($add[flen])
	{
		if($add[ftype]!="TEXT"&&$add[ftype]!="MEDIUMTEXT"&&$add[ftype]!="LONGTEXT")
		{
			$type.="(".$add[flen].")";
		}
	}
	$field=$add[f]." ".$type." NOT NULL".$def;
	$usql=$empire->query("alter table {$dbtbpre}ecms_".$tbname." change ".$add[oldf]." ".$field);
	$usql=$empire->query("alter table {$dbtbpre}ecms_".$tbname."_doc change ".$add[oldf]." ".$field);
	//采集表
	$usql=$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." change ".$add[oldf]." ".$field);
	$usql=$empire->query("alter table {$dbtbpre}ecms_infoclass_".$tbname." change zz_".$add[oldf]." zz_".$add[f]." text not null,change z_".$add[oldf]." z_".$add[f]." varchar(255) not null,change qz_".$add[oldf]." qz_".$add[f]." text not null,change save_".$add[oldf]." save_".$add[f]." varchar(10) not null;");
	//索引
	if($add[iskey]==1)
	{
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname." ADD INDEX(".$add[f].")");
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname."_doc ADD INDEX(".$add[f].")");
	}
	//删除旧索引
	if($add[oldiskey]==1&&$add[iskey]==0)
	{
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname." DROP INDEX ".$add[oldf]);
		$keysql=$empire->query("ALTER TABLE {$dbtbpre}ecms_".$tbname."_doc DROP INDEX ".$add[oldf]);
	}
	//替换代码
	if($add[f]<>$add[oldf]||$add[fform]<>$add[oldfform]||$add[fvalue]<>$add[oldfvalue]||$add[linkfieldval]<>$add[oldlinkfieldval]||$add[fformsize]<>$add[oldfformsize])
	{
		$fhtml=GetFform($add[fform],$add[f],$add[fvalue],$add[linkfieldval],$add[fformsize]);
	}
	else
	{
		$fhtml=$add[fhtml];
	}
	$cjhtml=GetCjform($add[fform],$add[f]);
	if($add[f]<>$add[oldf]||$add[fform]<>$add[oldfform]||$add[fvalue]<>$add[oldfvalue]||$add[fformsize]<>$add[oldfformsize])
	{
		$qfhtml=GetQFform($add[fform],$add[f],$add[fvalue],$add[fformsize]);
	}
	else
	{
		$qfhtml=$add[qfhtml];
	}
	//处理变量
	$add[iscj]=(int)$add[iscj];
	$add[myorder]=(int)$add[myorder];
	$tid=(int)$tid;
	$add[savetxt]=(int)$add[savetxt];
	$add[iskey]=(int)$add[iskey];
	$add[tobr]=(int)$add[tobr];
	$add[dohtml]=(int)$add[dohtml];
	$add[isonly]=(int)$add[isonly];
	$add[samedata]=(int)$add[samedata];

	$sql=$empire->query("update {$dbtbpre}enewsf set f='$add[f]',fname='$add[fname]',fform='$add[fform]',fhtml='".addslashes($fhtml)."',fzs='".addslashes($add[fzs])."',iscj=$add[iscj],cjhtml='".addslashes($cjhtml)."',myorder=$add[myorder],ftype='$add[ftype]',flen='$add[flen]',savetxt=$add[savetxt],fvalue='".addslashes($add[fvalue])."',iskey=$add[iskey],tobr=$add[tobr],dohtml=$add[dohtml],qfhtml='".addslashes($qfhtml)."',isonly='$add[isonly]',linkfieldval='$add[linkfieldval]',samedata='$add[samedata]',fformsize='$add[fformsize]' where fid='$add[fid]'");
	TogSaveTxtF();//公共变量
	if($add[savetxt]==1&&$add[f]=="newstext")
	{
		$tmpsql=$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." change newstext newstext mediumtext not null;");
	}
	//更新表单
	$record="<!--record-->";
    $field="<!--field--->";
	$like=$field.$add[oldf].$record;
	$newlike=$field.$add[f].$record;
	$slike=",".$add[oldf].",";
	$newslike=",".$add[f].",";
	$fsql=$empire->query("select mid,mtemp,cj,enter,tempvar,searchvar,tid,qenter,mustqenterf,qmtemp,listandf,listtempvar,canaddf,caneditf from {$dbtbpre}enewsmod where tid='$tid'");
	while($fr=$empire->fetch($fsql))
	{
		$and="";
		$enter=$fr['enter'];
		ChangeMForm($fr[mid],$fr[tid],$fr[mtemp]);
		ChangeQmForm($fr[mid],$fr[tid],$fr[qmtemp]);
		if($add[f]<>$add[oldf])
		{
			if(strstr($fr[cj],$like))
			{
				$cj=str_replace($like,$newlike,$fr[cj]);
				$and=",cj='$cj'";
				ChangeMCj($fr[mid],$fr[tid],$cj);
			}
			if(strstr($fr[enter],$like))
			{
				$enter=str_replace($like,$newlike,$fr[enter]);
				$and.=",enter='$enter'";
			}
			if(strstr($fr[qenter],$like))
			{
				$qenter=str_replace($like,$newlike,$fr[qenter]);
				$and.=",qenter='$qenter'";
			}
			if(strstr($fr[tempvar],$like))
			{
				$tempvar=str_replace($like,$newlike,$fr[tempvar]);
				$and.=",tempvar='$tempvar'";
			}
			if(strstr($fr[listtempvar],$like))
			{
				$listtempvar=str_replace($like,$newlike,$fr[listtempvar]);
				$and.=",listtempvar='$listtempvar'";
			}
			//搜索项
			if(strstr($fr[searchvar],$slike))
			{
				$searchvar=str_replace($slike,$newslike,$fr[searchvar]);
				$and.=",searchvar='$searchvar'";
			}
			if(strstr($fr[mustqenterf],$slike))
			{
				$mustqenterf=str_replace($slike,$newslike,$fr[mustqenterf]);
				$and.=",mustqenterf='$mustqenterf'";
			}
			if(strstr($fr[listandf],$slike))
			{
				$listandf=str_replace($slike,$newslike,$fr[listandf]);
				$and.=",listandf='$listandf'";
			}
			//可修改
			if(strstr($fr[caneditf],$slike))
			{
				$caneditf=str_replace($slike,$newslike,$fr[caneditf]);
				$and.=",caneditf='$caneditf'";
			}
			//可增加
			if(strstr($fr[canaddf],$slike))
			{
				$canaddf=str_replace($slike,$newslike,$fr[canaddf]);
				$and.=",canaddf='$canaddf'";
			}
		}
		//附件
		$myfilef=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"file");
		$myimgf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"img");
		$myflashf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"flash");
		//返回br项
		$mytobrf=ReturnMTobrF($enter,$tid,"tobr");
		$mydohtmlf=ReturnMTobrF($enter,$tid,"dohtml");
		$dosql=$empire->query("update {$dbtbpre}enewsmod set filef='$myfilef',imgf='$myimgf',flashf='$myflashf',tobrf='$mytobrf',dohtmlf='$mydohtmlf'".$and." where mid='$fr[mid]'");
	}
	if($usql&&$sql)
	{
		insert_dolog("fid=".$add[fid]."<br>f=".$add[f]);//操作日志
		printerror("EditFSuccess","db/ListF.php?tid=$tid&tbname=$tbname");
	}
	else
	{printerror("DbError","history.go(-1)");}
}

//修改数据表系统字段
function EditSysF($add,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$tbname=RepPostVar($add['tbname']);
	$fid=(int)$add['fid'];
	$f=RepPostVar($add['f']);
	if(!$fid||!$tid||!$tbname||!$f)
	{
		printerror("ErrorUrl","history.go(-1)");
	}
	CheckLevel($userid,$username,$classid,"f");//验证权限
	//处理变量
	$add[isonly]=(int)$add[isonly];
	$sql=$empire->query("update {$dbtbpre}enewsf set fhtml='".addslashes($add[fhtml])."',qfhtml='".addslashes($add[qfhtml])."',isonly='$add[isonly]' where fid='$fid'");
	TogSaveTxtF();//公共变量
	//更新表单
	$fsql=$empire->query("select mid,mtemp,tid,qmtemp from {$dbtbpre}enewsmod where tid='$tid'");
	while($fr=$empire->fetch($fsql))
	{
		ChangeMForm($fr[mid],$fr[tid],$fr[mtemp]);
		ChangeQmForm($fr[mid],$fr[tid],$fr[qmtemp]);
	}
	if($sql)
	{
		insert_dolog("fid=".$fid."<br>f=".$f);//操作日志
		printerror("EditFSuccess","db/EditSysF.php?tid=$tid&tbname=$tbname&fid=$fid");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//删除数据库字段
function DelF($fid,$tid,$tbname,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$tid;
	$tbname=RepPostVar($tbname);
	$fid=(int)$fid;
	if(empty($fid)||!$tid||!$tbname)
	{
		printerror("EmptyFid","history.go(-1)");
	}
	CheckLevel($userid,$username,$classid,"f");//验证权限
	//是否系统内部字段
	$cr=$empire->fetch1("select isadd,f from {$dbtbpre}enewsf where fid='$fid'");
	if(empty($cr[isadd]))
	{
		printerror("NotIsAdd","history.go(-1)");
	}
	$usql=$empire->query("alter table {$dbtbpre}ecms_".$tbname." drop COLUMN ".$cr[f]);
	$usql=$empire->query("alter table {$dbtbpre}ecms_".$tbname."_doc drop COLUMN ".$cr[f]);
	//采集表
	$usql=$empire->query("alter table {$dbtbpre}ecms_infotmp_".$tbname." drop COLUMN ".$cr[f]);
    $usql=$empire->query("alter table {$dbtbpre}ecms_infoclass_".$tbname." drop COLUMN zz_".$cr[f].",drop COLUMN z_".$cr[f].",drop COLUMN qz_".$cr[f].",drop COLUMN save_".$cr[f]);
	$sql=$empire->query("delete from {$dbtbpre}enewsf where fid='$fid'");
	TogSaveTxtF();//公共变量
	//删除模型中采集项
	$record="<!--record-->";
	$field="<!--field--->";
	$like=$field.$cr[f].$record;
	$slike=",".$cr[f].",";
	$dsql=$empire->query("select mid,cj,enter,tempvar,searchvar,tid,qenter,mustqenterf,listandf,listtempvar,filef,imgf,flashf,canaddf,caneditf,dohtmlf,tobrf from {$dbtbpre}enewsmod where tid='$tid' and (cj like '%".$like."%' or enter like '%".$like."%' or searchvar like '%".$slike."%' or tempvar like '%".$like."%' or listtempvar like '%".$like."%' or qenter like '%".$like."%' or mustqenterf like '%".$slike."%' or listandf like '%".$slike."%' or filef like '%".$slike."%' or imgf like '%".$slike."%' or flashf like '%".$slike."%' or canaddf like '%".$slike."%' or caneditf like '%".$slike."%' or dohtmlf like '%".$slike."%' or tobrf like '%".$slike."%')");
	while($r=$empire->fetch($dsql))
	{
		$cj="";
		$enter="";
		$tempvar="";
		$listtempvar="";
		$searchvar="";
		$qenter="";
		$mustqenterf="";
		$filef="";
		$imgf="";
		$flashf="";
		$listandf="";
		$canaddf="";
		$caneditf="";
		$dohtmlf="";
		$tobrf="";
		$re="";
		$re1="";
		$and="";
		$dh="";
		//采集
		if(strstr($r[cj],$like))
		{
			$re=explode($record,$r[cj]);
			for($i=0;$i<count($re)-1;$i++)
			{
				if(strstr($re[$i],$like))
				{continue;}
				$cj.=$re[$i].$record;
			}
			//更新采集表单
			ChangeMCj($r[mid],$r[tid],$cj);
			$and="cj='$cj'";
		}
		$dh="";
		//录入表单
		if(strstr($r[enter],$like))
		{
			$re1=explode($record,$r[enter]);
			for($i=0;$i<count($re1)-1;$i++)
			{
				if(strstr($re1[$i],$like))
				{continue;}
				$enter.=$re1[$i].$record;
			}
			if(!empty($and))
			{$dh=",";}
			$and.=$dh."enter='$enter'";
	    }
		$dh="";
		//投稿表单
		if(strstr($r[qenter],$like))
		{
			$re1=explode($record,$r[qenter]);
			for($i=0;$i<count($re1)-1;$i++)
			{
				if(strstr($re1[$i],$like))
				{continue;}
				$qenter.=$re1[$i].$record;
			}
			if(!empty($and))
			{$dh=",";}
			$and.=$dh."qenter='$qenter'";
	    }
		$dh="";
		//内容模板变量
		if(strstr($r[tempvar],$like))
		{
			$re1=explode($record,$r[tempvar]);
			for($i=0;$i<count($re1)-1;$i++)
			{
				if(strstr($re1[$i],$like))
				{continue;}
				$tempvar.=$re1[$i].$record;
			}
			if(!empty($and))
			{$dh=",";}
			$and.=$dh."tempvar='$tempvar'";
	    }
		$dh="";
		//列表模板变量
		if(strstr($r[listtempvar],$like))
		{
			$re1=explode($record,$r[listtempvar]);
			for($i=0;$i<count($re1)-1;$i++)
			{
				if(strstr($re1[$i],$like))
				{continue;}
				$listtempvar.=$re1[$i].$record;
			}
			if(!empty($and))
			{$dh=",";}
			$and.=$dh."listtempvar='$listtempvar'";
	    }
		$dh="";
		//搜索变量
		if(strstr($r[searchvar],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$searchvar=str_replace($slike,",",$r[searchvar]);
		    $and.=$dh."searchvar='$searchvar'";
		}
		//必填项
		$dh="";
		if(strstr($r[mustqenterf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$mustqenterf=str_replace($slike,",",$r[mustqenterf]);
		    $and.=$dh."mustqenterf='$mustqenterf'";
		}
		//加br项
		$dh="";
		if(strstr($r[tobrf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$tobrf=str_replace($slike,",",$r[tobrf]);
		    $and.=$dh."tobrf='$tobrf'";
		}
		$dh="";
		if(strstr($r[dohtmlf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$dohtmlf=str_replace($slike,",",$r[dohtmlf]);
		    $and.=$dh."dohtmlf='$dohtmlf'";
		}
		//可增可修改项
		$dh="";
		if(strstr($r[canaddf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$canaddf=str_replace($slike,",",$r[canaddf]);
		    $and.=$dh."canaddf='$canaddf'";
		}
		$dh="";
		if(strstr($r[caneditf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$caneditf=str_replace($slike,",",$r[caneditf]);
		    $and.=$dh."caneditf='$caneditf'";
		}
		//附件项
		$dh="";
		if(strstr($r[filef],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$filef=str_replace($slike,",",$r[filef]);
		    $and.=$dh."filef='$filef'";
		}
		$dh="";
		if(strstr($r[imgf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$imgf=str_replace($slike,",",$r[imgf]);
		    $and.=$dh."imgf='$imgf'";
		}
		$dh="";
		if(strstr($r[flashf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$flashf=str_replace($slike,",",$r[flashf]);
		    $and.=$dh."flashf='$flashf'";
		}
		//结合项
		$dh="";
		if(strstr($r[listandf],$slike))
		{
			if(!empty($and))
			{$dh=",";}
			$listandf=str_replace($slike,",",$r[listandf]);
		    $and.=$dh."listandf='$listandf'";
		}
		$dusql=$empire->query("update {$dbtbpre}enewsmod set ".$and." where mid='$r[mid]'");
    }
    if($usql&&$sql)
	{
		//操作日志
		insert_dolog("fid=".$fid."<br>f=".$cr[f]);
		printerror("DelFSuccess","db/ListF.php?tid=$tid&tbname=$tbname");
	}
	else
	{printerror("DbError","history.go(-1)");}
}

//修改字段顺序
function EditFOrder($fid,$myorder,$tid,$tbname,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$tid;
	$tbname=RepPostVar($tbname);
	//验证权限
	CheckLevel($userid,$username,$classid,"f");
	for($i=0;$i<count($myorder);$i++)
	{
		$newmyorder=(int)$myorder[$i];
		$usql=$empire->query("update {$dbtbpre}enewsf set myorder=$newmyorder where fid='$fid[$i]'");
    }
	printerror("EditFOrderSuccess","db/ListF.php?tid=$tid&tbname=$tbname");
}

//增加模型
function AddM($add,$cname,$cchange,$schange,$center,$cqenter,$menter,$listand,$ltempf,$ptempf,$canadd,$canedit,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$tbname=RepPostVar($add['tbname']);
	if(empty($add[mname])||!$tid||!$tbname)
	{
		printerror("EmptyM","history.go(-1)");
	}
	CheckLevel($userid,$username,$classid,"m");//验证权限

	$add[definfovoteid]=(int)$add[definfovoteid];
	//组合采集项
	$cj=TogMCj($cname,$cchange);
	//组合搜索项
    $searchvar=TogMSearch($cname,$schange);
	//组合必填项
	$mustqenterf=TogMustf($cname,$menter);
	//组合结合项
	$listandf=TogMustf($cname,$listand);
	//组合投稿项
	$qenter=TogMqenter($cname,$cqenter);
	//组合可增加项
	$canaddf=TogMustf($cname,$canadd);
	//组合可修改项
	$caneditf=TogMustf($cname,$canedit);
	//组合录入项
    $er=TogMEnter($cname,$center,$ltempf,$ptempf,$tid);
    $enter=$er[0];
	$listtempvar=$er[1];
	$tempvar=$er[2];
	//返回附件项
	$filef=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"file");
	$imgf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"img");
	$flashf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"flash");
	//返回br项
	$tobrf=ReturnMTobrF($enter,$tid,"tobr");
	$dohtmlf=ReturnMTobrF($enter,$tid,"dohtml");
	//自动生成表单
	if($add[mtype])
	{
		$add[mtemp]=ReturnMtemp($cname,$center);
	}
	if($add[qmtype])
	{
		$add[qmtemp]=ReturnQmtemp($cname,$cqenter);
	}
	$setandf=(int)$add['setandf'];
	$sql=$empire->query("insert into {$dbtbpre}enewsmod(mname,mtemp,mzs,cj,enter,tempvar,sonclass,searchvar,tid,tbname,qenter,mustqenterf,qmtemp,listandf,setandf,listtempvar,filef,imgf,flashf,qmname,canaddf,caneditf,dohtmlf,tobrf,definfovoteid) values('$add[mname]','".addslashes($add[mtemp])."','$add[mzs]','$cj','$enter','$tempvar','','$searchvar',$tid,'$tbname','$qenter','$mustqenterf','".addslashes($add[qmtemp])."','".addslashes($listandf)."','$setandf','$listtempvar','$filef','$imgf','$flashf','$add[qmname]','$canaddf','$caneditf','$dohtmlf','$tobrf',$add[definfovoteid]);");
	$mid=$empire->lastid();
	//更新表单
	ChangeMForm($mid,$tid,$add[mtemp]);
	ChangeQmForm($mid,$tid,$add[qmtemp]);
	//采集文件
	ChangeMCj($mid,$tid,$cj);
    if($sql)
	{
	    insert_dolog("mid=".$mid."<br>m=".$add[mname]);//操作日志
		printerror("AddMSuccess","db/ListM.php?tid=$tid&tbname=$tbname");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//修改模型
function EditM($add,$cname,$cchange,$schange,$center,$cqenter,$menter,$listand,$ltempf,$ptempf,$canadd,$canedit,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$tbname=RepPostVar($add['tbname']);
	$add[mid]=(int)$add[mid];
	if(empty($add[mname])||empty($add[mid])||!$tid||!$tbname)
	{
		printerror("EmptyM","history.go(-1)");
	}
	//验证权限
	CheckLevel($userid,$username,$classid,"m");
	$add[definfovoteid]=(int)$add[definfovoteid];
	//组合采集项
	$cj=TogMCj($cname,$cchange);
	//组合搜索项
    $searchvar=TogMSearch($cname,$schange);
	//组合必填项
	$mustqenterf=TogMustf($cname,$menter);
	//组合结合项
	$listandf=TogMustf($cname,$listand);
	//组合投稿项
	$qenter=TogMqenter($cname,$cqenter);
	//组合可增加项
	$canaddf=TogMustf($cname,$canadd);
	//组合可修改项
	$caneditf=TogMustf($cname,$canedit);
	//组合录入项
	$er=TogMEnter($cname,$center,$ltempf,$ptempf,$tid);
    $enter=$er[0];
	$listtempvar=$er[1];
	$tempvar=$er[2];
	//返回附件项
	$filef=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"file");
	$imgf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"img");
	$flashf=ReturnMFileF($enter,$dbtbpre."enewsf",$tid,"flash");
	//返回br项
	$tobrf=ReturnMTobrF($enter,$tid,"tobr");
	$dohtmlf=ReturnMTobrF($enter,$tid,"dohtml");
	//自动生成表单
	if($add[mtype])
	{
		$add[mtemp]=ReturnMtemp($cname,$center);
	}
	if($add[qmtype])
	{
		$add[qmtemp]=ReturnQmtemp($cname,$cqenter);
	}
	$setandf=(int)$add['setandf'];
	$sql=$empire->query("update {$dbtbpre}enewsmod set mname='$add[mname]',mtemp='".addslashes($add[mtemp])."',mzs='$add[mzs]',cj='$cj',enter='$enter',tempvar='$tempvar',searchvar='$searchvar',qenter='$qenter',mustqenterf='$mustqenterf',qmtemp='".addslashes($add[qmtemp])."',listandf='".addslashes($listandf)."',setandf=$setandf,listtempvar='$listtempvar',filef='$filef',imgf='$imgf',flashf='$flashf',qmname='$add[qmname]',canaddf='$canaddf',caneditf='$caneditf',dohtmlf='$dohtmlf',tobrf='$tobrf',definfovoteid=$add[definfovoteid] where mid='$add[mid]'");
	//更新表单
	ChangeMForm($add[mid],$tid,$add[mtemp]);
	ChangeQmForm($add[mid],$tid,$add[qmtemp]);
	//采集文件
	ChangeMCj($add[mid],$tid,$cj);
    if($sql)
	{
		//操作日志
	    insert_dolog("mid=".$add[mid]."<br>m=".$add[mname]);
		printerror("EditMSuccess","db/ListM.php?tid=$tid&tbname=$tbname");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//删除模型
function DelM($mid,$tid,$tbname,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$tid;
	$tbname=RepPostVar($tbname);
	$mid=(int)$mid;
	if(empty($mid)||!$tid||!$tbname)
	{
		printerror("EmptyMid","history.go(-1)");
	}
	//验证权限
	CheckLevel($userid,$username,$classid,"m");
	$r=$empire->fetch1("select mname from {$dbtbpre}enewsmod where mid='$mid'");
	$sql=$empire->query("delete from {$dbtbpre}enewsmod where mid='$mid'");
	DelFiletext("../data/html/".$mid.".php");
	DelFiletext("../data/html/q".$mid.".php");
	DelFiletext("../data/html/cj".$mid.".php");
	DelFiletext("../data/html/editcj".$mid.".php");
    if($sql)
	{
	    insert_dolog("mid=".$mid."<br>m=".$r[mname]);//操作日志
		printerror("DelMSuccess","db/ListM.php?tid=$tid&tbname=$tbname");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//建立初始表(2005-06-03)
function AddTableDefault($tbname,$tid){
	global $empire,$dbtbpre,$phome_db_dbchar;
	@include("db/DefaultTable.php");
	//复制存档表
	$otb=$dbtbpre."ecms_".$tbname;
	$tb=$otb."_doc";
	CopyEcmsTb($otb,$tb);
}

//复制数据表
function CopyNewTable($add,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$add['tid'];
	$newtbname=trim($add[newtbname]);
	if(!$tid||empty($newtbname)||!$add[tname])
	{
		printerror("EmptyTbname","");
	}
	CheckLevel($userid,$username,$classid,"table");//操作权限
	$tr=$empire->fetch1("select tbname from {$dbtbpre}enewstable where tid=$tid");
	if(!$tr[tbname])
	{
		printerror("EmptyTbname","");
	}
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$newtbname' limit 1");
	if($num)
	{
		printerror("ReTbname","history.go(-1)");
	}
	$sql=$empire->query("insert into {$dbtbpre}enewstable(tbname,tname,tsay,isdefault) values('$newtbname','$add[tname]','$add[tsay]',0);");
	$newtid=$empire->lastid();
	//复制表
	CopyEcmsTb($dbtbpre."ecms_".$tr['tbname'],$dbtbpre."ecms_".$newtbname);	//内容表
	CopyEcmsTb($dbtbpre."ecms_".$tr['tbname']."_doc",$dbtbpre."ecms_".$newtbname."_doc");	//归档表
	CopyEcmsTb($dbtbpre."ecms_infoclass_".$tr['tbname'],$dbtbpre."ecms_infoclass_".$newtbname);	//采集节点附加表
	CopyEcmsTb($dbtbpre."ecms_infotmp_".$tr['tbname'],$dbtbpre."ecms_infotmp_".$newtbname);	//采集数据临时表
	//字段数据
	$fsql=$empire->query("select * from {$dbtbpre}enewsf where tid=$tid order by fid");
	while($fr=$empire->fetch($fsql))
	{
		$usql=$empire->query("insert into {$dbtbpre}enewsf(f,fname,fform,fhtml,fzs,isadd,isshow,iscj,cjhtml,myorder,ftype,flen,dotemp,tid,tbname,savetxt,fvalue,iskey,tobr,dohtml,qfhtml,isonly,linkfieldval,samedata) values('$fr[f]','$fr[fname]','$fr[fform]','".addslashes(stripSlashes($fr['fhtml']))."','".addslashes(stripSlashes($fr[fzs]))."',$fr[isadd],$fr[isshow],$fr[iscj],'".addslashes(stripSlashes($fr[cjhtml]))."',$fr[myorder],'$fr[ftype]','$fr[flen]',$fr[dotemp],$newtid,'$newtbname',$fr[savetxt],'".addslashes(stripSlashes($fr[fvalue]))."',$fr[iskey],$fr[tobr],$fr[dohtml],'".addslashes(stripSlashes($fr[qfhtml]))."','$fr[isonly]','".addslashes(stripSlashes($fr[linkfieldval]))."','$fr[samedata]');");
	}
	TogSaveTxtF();//公共变量
	if($sql)
	{
		//操作日志
		insert_dolog("tid=".$tid."<br>tb=".$tr[tbname]."<br>newtid=".$newtid."<br>newtb=".$newtbname);
		printerror("CopyTbSuccess","db/ListTable.php");
	}
	else
	{
		printerror("DbError","");
	}
}

//建立数据表
function AddTable($add,$userid,$username){
	global $empire,$dbtbpre;
	$add[tbname]=trim($add[tbname]);
	if(!$add[tbname]||!$add[tname])
	{
		printerror("EmptyTbname","history.go(-1)");
    }
	//操作权限
	CheckLevel($userid,$username,$classid,"table");
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$add[tbname]' limit 1");
	if($num)
	{
		printerror("ReTbname","history.go(-1)");
	}
	$sql=$empire->query("insert into {$dbtbpre}enewstable(tbname,tname,tsay,isdefault) values('$add[tbname]','$add[tname]','$add[tsay]',0);");
	$tid=$empire->lastid();
	//初使化表
	AddTableDefault($add[tbname],$tid);
	if($sql)
	{
		//操作日志
		insert_dolog("tid=".$tid."<br>tbname=".$add[tbname]);
		printerror("AddTbSuccess","db/AddTable.php?enews=AddTable");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//修改数据表
function EditTable($add,$userid,$username){
	global $empire,$dbtbpre;
	$add[tbname]=trim($add[tbname]);
	$tid=(int)$add[tid];
	if(!$add[tbname]||!$add[tname]||!$tid)
	{
		printerror("EmptyTbname","history.go(-1)");
    }
	//操作权限
	CheckLevel($userid,$username,$classid,"table");
	//改变数据表名
	if($add[tbname]!=$add[oldtbname])
	{
		$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$add[tbname]' and tid<>$tid limit 1");
		if($num)
		{
			printerror("ReTbname","history.go(-1)");
		}
		//修改数据表名
		$usql=$empire->query("ALTER TABLE `{$dbtbpre}ecms_".$add[oldtbname]."` RENAME `{$dbtbpre}ecms_".$add[tbname]."`;");
	    $usql=$empire->query("update {$dbtbpre}enewsf set tbname='$add[tbname]' where tid='$tid'");
	    $usql=$empire->query("ALTER TABLE `{$dbtbpre}ecms_infoclass_".$add[oldtbname]."` RENAME `{$dbtbpre}ecms_infoclass_".$add[tbname]."`;");
		$usql=$empire->query("ALTER TABLE `{$dbtbpre}ecms_infotmp_".$add[oldtbname]."` RENAME `{$dbtbpre}ecms_infotmp_".$add[tbname]."`;");
		$usql=$empire->query("ALTER TABLE `{$dbtbpre}ecms_".$add[oldtbname]."_doc` RENAME `{$dbtbpre}ecms_".$add[tbname]."_doc`;");
		//修改栏目
		$usql=$empire->query("update {$dbtbpre}enewsclass set tbname='$add[tbname]' where tid='$tid'");
		$usql=$empire->query("update {$dbtbpre}enewszt set tbname='$add[tbname]' where tid='$tid'");
		$usql=$empire->query("update {$dbtbpre}enewsinfoclass set tbname='$add[tbname]' where tid='$tid'");
		$usql=$empire->query("update {$dbtbpre}enewsmod set tbname='$add[tbname]' where tid='$tid'");
		$usql=$empire->query("update {$dbtbpre}enewspublic set tbname='$add[tbname]' where tid='$tid'");
		//文本型
		TogSaveTxtF();
	}
	$sql=$empire->query("update {$dbtbpre}enewstable set tbname='$add[tbname]',tname='$add[tname]',tsay='$add[tsay]' where tid='$tid'");
	if($sql)
	{
		//操作日志
		insert_dolog("tid=".$tid."<br>tbname=".$add[tbname]);
		printerror("EditTbSuccess","db/ListTable.php");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//删除数据表
function DelTable($tid,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$tid;
	if(!$tid)
	{
		printerror("NotChangeTb","");
    }
	//操作权限
	CheckLevel($userid,$username,$classid,"table");
	$r=$empire->fetch1("select tid,tbname from {$dbtbpre}enewstable where tid='$tid'");
	if(empty($r[tid]))
	{
		printerror("ErrorUrl","");
	}
	$sql=$empire->query("delete from {$dbtbpre}enewstable where tid='$tid'");
	//删除数据表
	$dsql=$empire->query("DROP TABLE IF EXISTS {$dbtbpre}ecms_".$r[tbname].";");
	$dsql=$empire->query("DROP TABLE IF EXISTS {$dbtbpre}ecms_infoclass_".$r[tbname].";");
	$dsql=$empire->query("DROP TABLE IF EXISTS {$dbtbpre}ecms_infotmp_".$r[tbname].";");
	$dsql=$empire->query("DROP TABLE IF EXISTS {$dbtbpre}ecms_".$r[tbname]."_doc;");
	//删除数据
	$dsql=$empire->query("delete from {$dbtbpre}enewsf where tid='$tid'");
	$dsql=$empire->query("delete from {$dbtbpre}enewsmod where tid='$tid'");
	$dsql=$empire->query("delete from {$dbtbpre}enewsinfoclass where tid='$tid'");
	//文本型
	TogSaveTxtF();
	if($sql&&$dsql)
	{
		//操作日志
		insert_dolog("tid=".$tid."<br>tbname=".$r[tbname]);
		printerror("DelTbSuccess","db/ListTable.php");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//默认数据表
function DefaultTable($tid,$userid,$username){
	global $empire,$dbtbpre;
	$tid=(int)$tid;
	if(!$tid)
	{
		printerror("NotChangeDefaultTb","history.go(-1)");
    }
	//操作权限
	CheckLevel($userid,$username,$classid,"table");
	$r=$empire->fetch1("select tid,tbname from {$dbtbpre}enewstable where tid='$tid'");
	if(empty($r[tid]))
	{
		printerror("ErrorUrl","history.go(-1)");
	}
	$usql=$empire->query("update {$dbtbpre}enewstable set isdefault=0");
	$sql=$empire->query("update {$dbtbpre}enewstable set isdefault=1 where tid='$tid'");
	$upsql=$empire->query("update {$dbtbpre}enewspublic set tbname='$r[tbname]',tid='$tid'");
	GetConfig();
	if($sql&&$usql&&$upsql)
	{
		//操作日志
		insert_dolog("tid=".$tid."<br>tbname=".$r[tbname]);
		printerror("DefaultTableSuccess","db/ListTable.php");
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//更新模型表单文件
function ChangeAllModForm($add,$userid,$username){
	global $empire,$dbtbpre;
	//验证权限
	CheckLevel($userid,$username,$classid,"changedata");
	$sql=$empire->query("select mid,tid,mtemp,qmtemp,cj from {$dbtbpre}enewsmod");
	while($r=$empire->fetch($sql))
	{
		ChangeMForm($r[mid],$r[tid],$r[mtemp]);//更新表单
		ChangeQmForm($r[mid],$r[tid],$r[qmtemp]);//更新前台表单
		ChangeMCj($r[mid],$r[tid],$r[cj]);//采集表单
		//更新栏目导航
		if($add['ChangeClass']==1)
		{
			GetSearch($r[mid]);
		}
	}
	//操作日志
	insert_dolog("ChangeClass=$add[ChangeClass]");
	printerror("ChangeAllModFormSuccess","history.go(-1)");
}
?>